package com.codes.controller;

import com.codes.core.common.thread.pool.TestThread;
import com.codes.core.utils.IPUtils.IpUtil;
import com.codes.core.utils.macutils.SystemTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * @Description 
 * @Date 2020/7/24 11:44
 * @Created  peanut
 */
@Slf4j
@RequestMapping("/ip")
@RestController
public class IpGetCtr {
/*
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;*/
    @Autowired
    private TestThread testThread;

    @RequestMapping("/test")
    public Object getThread(HttpServletRequest request) throws Exception {
        // TestThread testThread = new TestThread();
//        threadPoolTaskExecutor.execute(new Runnable() {
//            @Override
//            public void run()  {
//
//                try {
//                    Thread.sleep(3003);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                for (int i = 0; i < 3; i++) {
//                    log.info(i+"");
//                    log.info(Thread.currentThread().getName());
//                }
//            }
//        });
        for (int i = 0; i < 5; i++) {
            testThread.printWord();
            testThread.doTaskTwo();
        }
        log.info("__________________ok");
        return "ok";
    }


    @RequestMapping("/get")
    public Object getIp2Mac(HttpServletRequest request) throws SocketException, UnknownHostException {

        String ipAddr = IpUtil.getIpAddr(request);

        log.info("ip:{}",ipAddr);
        String macAddr= IpUtil.getLocalMac(ipAddr);
        log.info("mac:{}",macAddr);
        return  ipAddr + ":_" + macAddr;


    }
    @RequestMapping("/get2")
    public Object getIp2Mac2(HttpServletRequest request) throws Exception {

        String mac;
        String os = SystemTool.getOSName();
        System.out.println(os);
        if (os.equals("windows 7")) {
              mac = SystemTool.getMACAddress();
            System.out.println(mac);
        } else if (os.startsWith("windows")) {
// 本地是windows
              mac = SystemTool.getWindowsMACAddress();
            System.out.println(mac);
        } else {
// 本地是非windows系统 一般就是unix
              mac = SystemTool.getUnixMACAddress();
            System.out.println(mac);
        }
        return  "真实的mac地址:" + mac;

    }


}